/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.projetjava.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.*;
import fr.unice.miage.projetjava.entities.Commande;
import fr.unice.miage.projetjava.entities.Users;
import java.util.Collection;

/**
 *
 * @author Olivier
 */
public class MenuClient extends Composite {

    private Users client = null;
    private VerticalPanel panelClient = new VerticalPanel();
    private HTML loginHTML = new HTML("Login : ");
    private HTML pwdHTML = new HTML("Password : ");
    private TextBox login = new TextBox();
    private PasswordTextBox pwd = new PasswordTextBox();
    private HTML connexionLink = new HTML("<a href='javascript:void(0)'>Se connecter</a>");
    private HTML inscriptionLink = new HTML("<a href='javascript:void(0)'>S'inscrire</a>");
    private HTML deconnexionLink = new HTML("<a href='javascript:void(0)'>Se déconnecter</a>");
    //private HTML compteLink = new HTML("<a href='javascript:void(0)'>Voir mon compte</a>");
    private HTML commandeLink = new HTML("<a href='javascript:void(0)'>Voir mes commandes</a>");
    private Label messageServeur = new Label();

    public MenuClient()
    {
        if(client.equals(null))
        {
            panelClient.add(loginHTML);
            panelClient.add(login);
            panelClient.add(pwdHTML);
            panelClient.add(pwd);
            panelClient.add(connexionLink);
            panelClient.add(inscriptionLink);
        }
        else
        {
            panelClient.add(messageServeur);
            //panelClient.add(compteLink);
            panelClient.add(commandeLink);
            panelClient.add(deconnexionLink);
        }
        panelClient.addStyleName(".gwt-DialogBox");


        // Create an asynchronous callback to handle the result.
        final AsyncCallback callbackConnexion = new AsyncCallback() {
            public void onSuccess(Object result) {
                Users user = (Users) result;
                if(user.equals(null))
                {
                    Popup p = new Popup("Veuillez vérifier vos identifiants", "auto");
                    int left = panelClient.getAbsoluteLeft() + 10;
                    int top = panelClient.getAbsoluteTop() + 10;
                    p.setPopupPosition(left, top);
                    p.show();
                }
                else
                {
                    messageServeur.setText("Bonjour "+user.getFirstName()+" "+user.getLastName());
                    client = user;
                    panelClient.remove(loginHTML);
                    panelClient.remove(login);
                    panelClient.remove(pwdHTML);
                    panelClient.remove(pwd);
                    panelClient.remove(connexionLink);
                    panelClient.remove(inscriptionLink);
                    panelClient.add(messageServeur);
                    //panelClient.add(compteLink);
                    panelClient.add(commandeLink);
                    panelClient.add(deconnexionLink);
                }
            }

            public void onFailure(Throwable caught) {
                messageServeur.setText("Communication failed");
            }
        };

        final AsyncCallback callbackDeconnexion = new AsyncCallback() {
            public void onSuccess(Object result) {
                    client = null;
                    panelClient.remove(messageServeur);
                    //panelClient.remove(compteLink);
                    panelClient.remove(commandeLink);
                    panelClient.remove(deconnexionLink);
                    panelClient.add(loginHTML);
                    panelClient.add(login);
                    panelClient.add(pwdHTML);
                    panelClient.add(pwd);
                    panelClient.add(connexionLink);
                    panelClient.add(inscriptionLink);
            }

            public void onFailure(Throwable caught) {
                messageServeur.setText("Communication failed");
            }
        };

        final AsyncCallback callbackCommande = new AsyncCallback() {
            public void onSuccess(Object result) {
                Collection<Commande> listCommande = (Collection<Commande>)result;
                if(listCommande.size()==0)
                {
                    Popup p = new Popup("Vous n'avez pas de commande.", "auto");
                    int left = panelClient.getAbsoluteLeft() + 10;
                    int top = panelClient.getAbsoluteTop() + 10;
                    p.setPopupPosition(left, top);
                    p.show();
                }
                else
                {
                    DialogBox dlg = new Dialog2(listCommande);
                    int left = inscriptionLink.getAbsoluteLeft() + 10;
                    int top = inscriptionLink.getAbsoluteTop() + 10;
                    dlg.setPopupPosition(left, top);
                    dlg.show();
                }
            }

            public void onFailure(Throwable caught) {
                messageServeur.setText("Communication failed");
            }
        };

        // Listen for the button clicks
        connexionLink.addClickListener(new ClickListener(){
            public void onClick(Widget w) {
                // Make remote call. Control flow will continue immediately and later
                // 'callback' will be invoked when the RPC completes.
                getService().connexionUser(login.getText(), pwd.getText(), callbackConnexion);
            }
        });

        deconnexionLink.addClickListener(new ClickListener(){
            public void onClick(Widget w) {
                // Make remote call. Control flow will continue immediately and later
                // 'callback' will be invoked when the RPC completes.
                getService().deconnexionUser(callbackDeconnexion);
            }
        });

        inscriptionLink.addClickListener(new ClickListener(){
            public void onClick(Widget w) {
                // Make remote call. Control flow will continue immediately and later
                // 'callback' will be invoked when the RPC completes.
                //getService().inscriptionUser(callbackInscription);
                DialogBox dlg = new Dialog();
                int left = inscriptionLink.getAbsoluteLeft() + 10;
                int top = inscriptionLink.getAbsoluteTop() + 10;
                dlg.setPopupPosition(left, top);
                dlg.show();
            }
        });

        commandeLink.addClickListener(new ClickListener(){
            public void onClick(Widget w) {
                // Make remote call. Control flow will continue immediately and later
                // 'callback' will be invoked when the RPC completes.
                getService().commandeUser(client, callbackCommande);
            }
        });
        
        initWidget(panelClient);
    }

    public static MenuClientTestAsync getService(){
        // Create the client proxy. Note that although you are creating the
        // service interface proper, you cast the result to the asynchronous
        // version of
        // the interface. The cast is always safe because the generated proxy
        // implements the asynchronous interface automatically.
        MenuClientTestAsync service = (MenuClientTestAsync) GWT.create(MenuClientTest.class);
        // Specify the URL at which our service implementation is running.
        // Note that the target URL must reside on the same domain and port from
        // which the host page was served.
        //
        ServiceDefTarget endpoint = (ServiceDefTarget) service;
        String moduleRelativeURL = GWT.getModuleBaseURL() + "menuclienttest";
        endpoint.setServiceEntryPoint(moduleRelativeURL);
        return service;
    }

}
